package db;


import java.util.*;
import javax.swing.JOptionPane;

//////////////////////////////////////////////////////////////////////////
//// DataBase

/** Classe base contendo os dados das analises de atores e classes
Inicialmente, carrega os indices do arquivo. So carrega os dados 
quando necessario.

@author Ivan Jeukens
@version v 0.1 28/2/2002
*/
public class DataBase {

    public DataBase() {
        _data = new HashMap();
        _tableModel = new DBTableModel();
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    public void setBasePath(String basePath) {
        _basePath = basePath;
    }

    public Iterator namesIterator() {
        Set keys = _data.keySet();
        return keys.iterator();
    }
    
    public Iterator valuesIterator() {
        return _data.values().iterator();
    }

    public void add(dbInfo info) {
        _data.put(info.getName(), info);
        _tableModel.add(info.getSummary());
    }

    /* Adiciona apenas um sumario do info. Carrega os dados
    sob demanda */
    public void addIndex(dbInfoSummary sum) {
        _data.put(sum.getName(), sum);
    }

    /* Se nao tiver nada, ou ja for o dbInfo, retorna null */
    public dbInfoSummary getSummary(String name) {
        Object obj = _data.get(name);
        if(obj == null) return null;

        if(obj instanceof dbInfoSummary) {
            return (dbInfoSummary) obj;
        }
        return null;
    }

    public Object get(String name) {
        return _data.get(name);
    }

    public boolean contains(String name) {
        return _data.containsKey(name);
    }

    // A classes especificas de cada tipo de database tem que sobrescrever
    // esse metodo.
    public dbInfo getInfo(String name) {
        return null;
    }

    public DBTableModel getTableModel() {
        return _tableModel;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer("Data Base , path = " + _basePath + "\n");
        Iterator ite = namesIterator();
        while(ite.hasNext()) {
            String name = (String) ite.next();
            buf.append("index -> " + name + "\n");
        }
        return buf.toString();
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    private void _updateTableModel() {
        Collection values = _data.values();
        Iterator ite = values.iterator();
        while(ite.hasNext()) {
            dbInfoSummary sum = (dbInfoSummary) ite.next();
            _tableModel.add(sum);
        }
        _tableModel.fireTableDataChanged();
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private variables                  ////

    protected HashMap _data;
    protected DBTableModel _tableModel;
    protected String _basePath;
}
